package com.ldy.core.web;

import java.util.Date;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ldy.core.common.Result;
import com.ldy.core.entity.BaseArticle;
import com.ldy.core.service.BaseArticleService;
import com.ldy.core.vo.ParamDateVO;

/**
 * 文字内容管理
 */
@RestController
@RequestMapping("/article")
@CrossOrigin(allowCredentials = "true", allowedHeaders = "*")
public class ArticleController {
	
	private static final Logger logger = LoggerFactory.getLogger(ArticleController.class);
	
	@Autowired
	private BaseArticleService articleService;

	/**
	 * 列表分页查询
	 * @param current
	 * @param size
	 * @param article
	 * @return
	 */
	@PostMapping("/selectPage")
	public Result selectPage(int current, int size, BaseArticle article, ParamDateVO vo) {
		Page<BaseArticle> page = new Page<BaseArticle>(current, size);
		QueryWrapper<BaseArticle> query = new QueryWrapper<>();
		if (StringUtils.hasText(article.getTitle())) {
			query.like("title", article.getTitle());
		}
		if (StringUtils.hasText(article.getType())) {
			query.eq("type", article.getType());
		}
		if (StringUtils.hasText(article.getSubType())) {
			query.eq("sub_type", article.getSubType());
		}
		if (StringUtils.hasText(vo.getStartDate())) {
			query.ge("create_time", vo.getStartDate());
		}
		if (StringUtils.hasText(vo.getEndDate())) {
			query.lt("create_time",  vo.getEndDate());
		}
		query.select("create_time", "id", "sub_type", "title", "type", "remarks", "last_update_time");
		query.orderByDesc("create_time");
		return Result.success(articleService.page(page, query));
	}
	
	/**
	 * 列表查询
	 * @return
	 */
	@PostMapping("/selectList")
	public Result selectList(String id) {
		QueryWrapper<BaseArticle> query = new QueryWrapper<>();
		if (StringUtils.hasText(id)) {
			query.eq("id", id);
		}
		query.orderByDesc("create_time");
		return Result.success(articleService.list(query));
	}
	
	/**
	 * 新增
	 * 
	 * @return
	 */
	@PostMapping("/insert")
	public Result insert(BaseArticle info) {
		logger.info("method: {}, params:[info: {}]", "insert", JSONObject.toJSONString(info));
		Date now = new Date();
		info.setLastUpdateTime(now);
		info.setType("朗县");
		articleService.save(info);
		return Result.success();
	}
	
	/**
	 * 更新
	 * 
	 * @return
	 */
	@PostMapping("/update")
	public Result update(BaseArticle info) {
		logger.info("method: {}, params:[info: {}]", "update", JSONObject.toJSONString(info));
		Date now = new Date();
		info.setLastUpdateTime(now);
		articleService.updateById(info);
		return Result.success();
	}
	
	/**
	 * 删除
	 * 
	 * @return
	 */
	@PostMapping("/delete")
	public Result delete(String id) {
		logger.info("method: {}, params:[id: {}]", "delete", id);
		articleService.removeById(id);
		return Result.success();
	}
}
